﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AvalonDock;
using AvalonDock.Layout.Serialization;

namespace 多协议通信平台.Views.Protocal
{
    /// <summary>
    /// Mc3eView.xaml 的交互逻辑
    /// </summary>
    public partial class SecsGemView : UserControl
    {
        private TreeViewItem _selectedItem;
        public SecsGemView()
        {
            InitializeComponent();
            Loaded += SecsGemView_Loaded;
        }

        #region 布局保存重置
        private string _initialLayout;

        private void SecsGemView_Loaded(object sender, RoutedEventArgs e)
        {
            var serializer = new XmlLayoutSerializer(dockingManager);
            using (var writer = new StringWriter())
            {
                serializer.Serialize(writer);
                _initialLayout = writer.ToString(); // 保存初始布局
            }

        }
        
        private void ResetLayout(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(_initialLayout))
            {
                var serializer = new XmlLayoutSerializer(dockingManager);
                using (var reader = new StringReader(_initialLayout))
                {
                    serializer.Deserialize(reader); // 恢复到初始状态
                }
            }
        }
        #endregion

        // 获取当前选中的 TreeViewItem
        private void MyTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            _selectedItem = MyTreeView.SelectedItem as TreeViewItem;
        }

        // 添加子项
        private void AddChild_Click(object sender, RoutedEventArgs e)
        {
            if (_selectedItem != null)
            {
                TreeViewItem newItem = new TreeViewItem() { Header = "新子项" };
                _selectedItem.Items.Add(newItem);
                _selectedItem.IsExpanded = true;
            }
        }

        // 删除当前项
        private void DeleteItem_Click(object sender, RoutedEventArgs e)
        {
            if (_selectedItem == null) return;

            var parent = GetParent(_selectedItem);
            if (parent is TreeViewItem parentItem)
            {
                parentItem.Items.Remove(_selectedItem);
            }
            else if (parent is TreeView tree)
            {
                tree.Items.Remove(_selectedItem);
            }
        }

        // 获取父项方法
        private ItemsControl GetParent(TreeViewItem item)
        {
            DependencyObject parent = VisualTreeHelper.GetParent(item);
            while (parent != null && !(parent is TreeViewItem || parent is TreeView))
            {
                parent = VisualTreeHelper.GetParent(parent);
            }
            return parent as ItemsControl;
        }
    }
}
